package com.zoewin.zephyr.reportjasper.plugins;

import org.apache.commons.lang3.ClassUtils;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * Created by WQL on 2020/9/4
 * CEASE TO STRUGGLE AND YOU CEASE TO LIVE
 */
public class ReflectionUtils {
    public ReflectionUtils() {
    }

    public static Object getValue(String fieldName, Object obj) {
        Objects.requireNonNull(obj, "Class must not be null");
        Objects.requireNonNull(fieldName, "Name must not be null");
        Field field = findField(obj.getClass(), fieldName);
        if (null == field) {
            throw new RuntimeException("No Such field " + fieldName + " from class" + ClassUtils.getShortClassName(obj.getClass()));
        } else {
            try {
                field.setAccessible(true);
                return field.get(obj);
            } catch (Exception var4) {
                throw new RuntimeException();
            }
        }
    }

    public static Field findField(Class<?> clazz, String name) {
        Objects.requireNonNull(clazz, "Class must not be null");
        Objects.requireNonNull(name, "Name must not be null");

        for(Class searchType = clazz; Object.class != searchType && searchType != null; searchType = searchType.getSuperclass()) {
            try {
                Field field = searchType.getDeclaredField(name);
                if (null != field) {
                    return field;
                }
            } catch (NoSuchFieldException var5) {
            }
        }

        return null;
    }
}
